<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible"
	append-to-body
	>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="选择类型" prop="type">
      <el-radio-group v-model="dataForm.type" @change="typeChange">
        <el-radio :label="1">优惠券</el-radio>
        <el-radio :label="2">早餐券</el-radio>
        <el-radio :label="3">免房券</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="会员等级" prop="levelId">
      <el-select v-model="dataForm.levelId" filterable placeholder="请选择">
        <el-option
          v-for="item in levelEntities"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      </el-input>
    </el-form-item>
    <el-form-item label="选择券" prop="couponsCashId">
      <el-select v-if="dataForm.type == 1" v-model="dataForm.couponsCashId" filterable placeholder="请选择">
        <el-option
          v-for="item in cashEntities"
          :key="item.id"
          :label="item.name"
          :value="item.id"
          :disabled="item.disabled">
              <span style="float: left">{{ item.name }}</span>
              <span v-if="item.disabled" style="float: right;">已过期</span>
        </el-option>
      </el-select>
      <el-select v-if="dataForm.type == 2" v-model="dataForm.couponsCashId" filterable placeholder="请选择">
        <el-option
          v-for="item in breakfastEntities"
          :key="item.id"
          :label="item.name"
          :value="item.id"
          :disabled="item.disabled">
              <span style="float: left">{{ item.name }}</span>
              <span v-if="item.disabled" style="float: right;">已过期</span>
        </el-option>
      </el-select>
      <el-select v-if="dataForm.type == 3" v-model="dataForm.couponsCashId" filterable placeholder="请选择">
        <el-option
          v-for="item in couponsEntities"
          :key="item.id"
          :label="item.name"
          :value="item.id"
          :disabled="item.disabled">
              <span style="float: left">{{ item.name }}</span>
              <span v-if="item.disabled" style="float: right;">已过期</span>
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="数量" prop="count">
      <el-input-number v-model="dataForm.count" :min="1" :step="1" step-strictly></el-input-number>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: 1,
          levelId: '',
          couponsCashId: '',
          count: 1,
          sellerId: '',
          eventId: ''
        },
        levelEntities:[],
        cashEntities:[],
        breakfastEntities:[],
        couponsEntities:[],
        dataRule: {
          type: [
            { required: true, message: '类型（1优惠券）不能为空', trigger: 'blur' }
          ],
          levelId: [
            { required: true, message: '会员类型不能为空', trigger: 'blur' }
          ],
          couponsCashId: [
            { required: true, message: '优惠券不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '门店不能为空', trigger: 'blur' }
          ],
          eventId: [
            { required: true, message: '事件id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      typeChange(val) {
        this.dataForm.couponsCashId = ""
      },
      init (id,eventId) {
        this.dataForm.id = id || 0
        this.dataForm.eventId = eventId
        this.getdatalist();
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelcouponseventdetailed/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.tHotelCouponsEventDetailed.type
                this.dataForm.levelId = data.tHotelCouponsEventDetailed.levelId
                this.dataForm.couponsCashId = data.tHotelCouponsEventDetailed.couponsCashId
                this.dataForm.count = data.tHotelCouponsEventDetailed.count
                this.dataForm.sellerId = data.tHotelCouponsEventDetailed.sellerId
                this.dataForm.eventId = data.tHotelCouponsEventDetailed.eventId
              }
            })
          }
        })
      },
      //获取需要的数据
      getdatalist() {
        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelcouponseventdetailed/getdatalist`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            this.levelEntities = data.levelEntities
            this.cashEntities = data.cashEntities
            this.breakfastEntities = data.breakfastEntities
            this.couponsEntities = data.couponsEntities
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelcouponseventdetailed/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'levelId': this.dataForm.levelId,
                'couponsCashId': this.dataForm.couponsCashId,
                'count': this.dataForm.count,
                'sellerId': this.dataForm.sellerId,
                'eventId': this.dataForm.eventId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
